package com.wn.ticket.web.backstage;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.wn.ticket.common.PageBean;
import com.wn.ticket.common.ResponseEntity;
import com.wn.ticket.entity.Order;
import com.wn.ticket.entity.Perform;
import com.wn.ticket.entity.User;
import com.wn.ticket.service.OrderService;
import com.wn.ticket.service.PerformService;
import com.wn.ticket.service.UserService;
import com.wn.ticket.vo.WelcomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 用户后台首页数据展示
 * @author wuqingting
 * @date 2022/9/1
 */
@RestController
@RequestMapping("/backstage/welcome")
public class WelcomeController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PerformService performService;

    @RequestMapping("/getPerm/{userDateValue},{orderDateValue}")
    public ResponseEntity getPerm(@PathVariable Integer userDateValue,@PathVariable Integer orderDateValue){
        WelcomeVo welcomeVo = new WelcomeVo();
        int userNumber = userService.getCount();
        Integer[] users = statisticsRegistTime(userDateValue);
        Integer[] orders = statisticsOrderTime(orderDateValue);
        String[] userDates = dates(userDateValue);
        String[] orderDates = dates(orderDateValue);
        int allOrderNumber = allOrderNumber();
        BigDecimal totalPrice = allOrderTotalPrice();
        int sellingPerform = allSellingPerform();

        welcomeVo.setUserData(users);
        welcomeVo.setUserOpinionData(userDates);
        welcomeVo.setOrderOpinionData(orderDates);
        welcomeVo.setOrderData(orders);
        welcomeVo.setUserNumber(userNumber);
        welcomeVo.setOrderNumber(allOrderNumber);
        welcomeVo.setTotalPrice(totalPrice);
        welcomeVo.setSellPerform(sellingPerform);
        return new ResponseEntity(welcomeVo);
    }

    /**
     * 统计当前日期到days天前注册的用户
     * @param days
     * @return days天中,每天注册的用户数量,从-days天至今天
     */
    private Integer[] statisticsRegistTime(int days){
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.DAY_OF_MONTH, -days); //当前时间减去days天，即days天前的时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        String time = simpleDateFormat.format(calendar.getTime());

        List<User> userList = userService.getByRegistTime(time);//获取在大于当天日期-days日期注册的用户
        List<Integer> arrayList = new ArrayList<>();
        for (int i=0;i<days;i++){
            arrayList.add(0);
        }

        for (User u:userList){
            calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.DAY_OF_MONTH, -days); //当前时间减days天
            for (int i=days;i>0;i--) {
                DateTime inTime = DateUtil.parse(u.getRegistTime());//用户注册日期

                time=simpleDateFormat.format(calendar.getTime());
                DateTime startTime = DateUtil.parse(time);//倒数i天的开始日期(0时)

                calendar.add(Calendar.DAY_OF_MONTH,1);//加一天
                time=simpleDateFormat.format(calendar.getTime());
                DateTime endTime = DateUtil.parse(time);//倒数i+1天的开始日期(0时)

                boolean in = DateUtil.isIn(inTime, startTime, endTime);//判断时间是否在其之间
                if (in){
                    arrayList.set(days-i,arrayList.get(days-i)+1);//当前日期的统计天数加一
                    break;
                }
            }
        }

        Integer[] ints = new Integer[days];
        return arrayList.toArray(ints);
    }

    /**
     * 统计当前日期到days天前下单的用户
     * @param days
     * @return days天中,每天注册的用户数量,从-days天至今天
     */
    private Integer[] statisticsOrderTime(int days){
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置格式
        String endTime = simpleDateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, -days); //当前时间减去days天，即days天前的时间
        String startTime = simpleDateFormat.format(calendar.getTime());

        Order order = new Order();
        order.setStartTime(startTime);
        order.setEndTime(endTime);

        List<Order> orderList = orderService.getByTimeNotIncludeCancel(order);

        List<Integer> arrayList = new ArrayList<>();
        for (int i=0;i<days;i++){
            arrayList.add(0);
        }

        for (Order o:orderList){
            calendar = Calendar.getInstance(); //创建Calendar 的实例
            calendar.add(Calendar.DAY_OF_MONTH, -days); //当前时间减days天
            for (int i=days;i>0;i--) {
                DateTime inTime = DateUtil.parse(o.getBuyTime());//下单日期

                String time = simpleDateFormat.format(calendar.getTime());
                DateTime dateStartTime = DateUtil.parse(time);//倒数i天的开始日期(0时)

                calendar.add(Calendar.DAY_OF_MONTH,1);//加一天
                time=simpleDateFormat.format(calendar.getTime());
                DateTime dateEndTime = DateUtil.parse(time);//倒数i+1天的开始日期(0时)

                boolean in = DateUtil.isIn(inTime, dateStartTime, dateEndTime);//判断时间是否在其之间
                if (in){
                    arrayList.set(days-i,arrayList.get(days-i)+1);//当前日期的统计天数加一
                    break;
                }
            }
        }

        Integer[] ints = new Integer[days];
        return arrayList.toArray(ints);
    }

    /**
     * 统计所有未取消订单的总金额
     * @return
     */
    private BigDecimal allOrderTotalPrice(){
        Order order = new Order();
        List<Order> orderList = orderService.getByTimeNotIncludeCancel(order);
        BigDecimal totalPrice = new BigDecimal("0");
        for (Order o:orderList){
            totalPrice = totalPrice.add(o.getTotalPrice());
        }

        return totalPrice;
    }

    /**
     * 统计所有未取消订单的数量
     * @return
     */
    private int allOrderNumber(){
        Order order = new Order();
        List<Order> orderList = orderService.getByTimeNotIncludeCancel(order);

        return orderList.size();
    }

    /**
     * 统计所有在售演出的数量
     * @return
     */
    private int allSellingPerform(){
        Perform perform = new Perform();
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置格式
        String time = simpleDateFormat.format(calendar.getTime());

        perform.setSellTime(time);//开售时间小于当前时间
        perform.setStartTime(time);//演出时间大于当前时间
        PageBean<Perform> pageBean = (PageBean<Perform>) performService.getPageByCondition(perform, 1, Integer.MAX_VALUE);

        return pageBean.getData().size();
    }

    /**
     * 根据days生成一个日期数组,用于存放图表下方的日期轴
     * @param days
     * @return
     */
    private String[] dates(int days){
        String[] dates = new String[days];//用于存放图表下方的日期轴
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.DAY_OF_MONTH, -days);//当前时间减去days天
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M-dd");//设置格式
        for (int i=0;i<days;i++){
            calendar.add(Calendar.DAY_OF_MONTH, 1); //当前时间加一天
            String time = simpleDateFormat.format(calendar.getTime());
            dates[i]=time;
        }

        return dates;
    }

}
